package com.easy.tool;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import android.widget.Toast;

import androidx.annotation.StringRes;

public class ToastUtils {
    public static boolean isShow = true;
    private static Handler sHandler = new Handler(Looper.getMainLooper());
    private static Toast sToast;
    private ToastUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, CharSequence message) {
        if (isShow) {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, @StringRes int message) {
        if (isShow) {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, CharSequence message) {
        if (isShow) {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, @StringRes int message) {
        if (isShow) {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, CharSequence message, int duration) {
        if (isShow){
            Toast.makeText(context, message, duration).show();
        }
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, @StringRes int message, int duration) {
        if (isShow){
            Toast.makeText(context, message, duration).show();
        }
    }

    /**
     * 任意线程发送Toast
     *
     * @param context
     * @param text
     */
    public static void showSafeToast(final Context context, final String text) {
        if (isShow){
            sHandler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * 同一个Toast直接修改内容
     *
     * @param context
     * @param text
     */
    public static void showSingleToast(Context context, String text) {
        if (isShow){
            if (sToast == null) {
                sToast = Toast.makeText(context, text, Toast.LENGTH_LONG);
            }
            sToast.setText(text);
            sToast.show();
        }
    }
}
